#coding:utf8

'''
微信公众平台api，python sample_sdk

Author: @sleepiforest

Company: Tencent

'''

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

import urllib2
import json
import os
import cPickle as pickle
from time import time as now
from copy import deepcopy

import sys
reload(sys)
sys.setdefaultencoding('utf8')

class OAuthBase(object):
    def __init__(self, host, appid, secret):
        self.host = host
        self.appid = appid
        self.secret = secret

    @staticmethod
    def deco_parse_json_resp(func):
        def __func(self, *args, **kwargs):
            json_resp = unicode(func(self, *args, **kwargs), 'utf8')
            print 'json_response:', json_resp
            return json.loads(json_resp)
        return __func
            
class MMOpenApi(OAuthBase):
    def __init__(self, appid, secret):
        super(MMOpenApi, self).__init__("https://api.weixin.qq.com", appid, secret)
        #super(MMOpenApi, self).__init__("http://api.weixin.qq.com", appid, secret)
        #super(MMOpenApi, self).__init__("http://10.193.26.78:12813", appid, secret)
        self.__access_token = ""

    def __load_access_token(self):
        if not self.__access_token and os.access('access_token', os.R_OK):
            data = pickle.load(file('access_token', 'r'))
            if self.appid in data:
                self.__access_token = data[self.appid]

        if self.__access_token and self.__access_token['expire_time'] > now():
            return self.__access_token['access_token']

    def __dump_access_token(self):
        data = {}
        if os.access('access_token', os.R_OK):
            data = pickle.load(file('access_token', 'r'))

        data[self.appid] = self.__access_token
        pickle.dump(data, file('access_token', 'w'))

    def get_post_json(self, dc):
        return json.dumps(dc, ensure_ascii=False).encode('utf8')

    @property
    def access_token(self):
        if self.__load_access_token():
            return self.__access_token['access_token'] 

        url = '%s/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (self.host, self.appid, self.secret)
        resp = unicode(urllib2.urlopen(url).read(), 'utf8')
        print resp
        self.__access_token = json.loads(resp)
        if 'access_token' in self.__access_token:
            self.__access_token['expire_time'] = now() + self.__access_token['expires_in']
            self.__dump_access_token()
            return self.__access_token['access_token']
        else:
            print 'error resp: ', resp
            self.__access_token = None
            return None

    @OAuthBase.deco_parse_json_resp
    def get_qrcode_ticket(self, scene_id, expire_seconds=1800):
        url = "%s/cgi-bin/qrcode/create?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json( {"expire_seconds": expire_seconds, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": scene_id}}} )
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def get_limit_qrcode_ticket(self, scene_id):
        url = "%s/cgi-bin/qrcode/create?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": scene_id}}} )
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def get_card_qrcode_ticket(self, card_id, code="", openid="", is_unique_code = False):
        url = "%s/cgi-bin/qrcode/create?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"action_name": "QR_CARD", "action_info": {"card": {"card_id": card_id, "code": code, "openid": openid, "is_unique_code": is_unique_code}}} )
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def get_userinfo(self, openid, lang="zh_CN"):
        url = '%s/cgi-bin/user/info?access_token=%s&openid=%s&lang=%s' % (self.host, self.access_token, openid, lang)
        return urllib2.urlopen(url).read()
    
    @OAuthBase.deco_parse_json_resp
    def get_fans(self, next_openid):
        url = "%s/cgi-bin/user/get?access_token=%s&next_openid=%s" % (self.host, self.access_token, next_openid)
        return urllib2.urlopen(url).read()

    def get_all_fans(self):
        res = []
        next_openid = ""
        while True:
            resp = self.get_fans(next_openid)
            next_openid = resp['next_openid']
            if not next_openid:
                return res
            res.extend(resp['data']['openid'])

    @OAuthBase.deco_parse_json_resp
    def upload_media(self, filename, type):
        register_openers()
        data, headers = multipart_encode({"media": file(filename, "rb")})
        url = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s' % (self.access_token, type)
        request = urllib2.Request(url, data, headers)
        return urllib2.urlopen(request).read()

    @OAuthBase.deco_parse_json_resp
    def upload_media_news(self, data):
        url = 'http://file.api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=%s' % (self.access_token)
        data = self.get_post_json(data)
        return urllib2.urlopen(url, data).read()

    def get_media(self, media_id):
        url = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s' % (self.access_token, media_id)
        return urllib2.urlopen(url).read()

    @OAuthBase.deco_parse_json_resp
    def get_groups(self):
        url = "%s/cgi-bin/groups/get?access_token=%s" % (self.host, self.access_token)
        return urllib2.urlopen(url).read()

    @OAuthBase.deco_parse_json_resp
    def create_group(self, group_name):
        url = "%s/cgi-bin/groups/create?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"group":{"name": group_name}})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def update_group(self, group_id, group_name):
        url = "%s/cgi-bin/groups/update?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"group":{"id": group_id, "name": group_name }})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def update_group_member(self, openid, group_id):
        url = "%s/cgi-bin/groups/members/update?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"openid": openid, "to_groupid": group_id})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def create_menu(self, py_data):
        url = "%s/cgi-bin/menu/create?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json(py_data)
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def get_menu(self):
        url = "%s/cgi-bin/menu/get?access_token=%s" % (self.host, self.access_token)
        return urllib2.urlopen(url).read()

    @OAuthBase.deco_parse_json_resp
    def delete_menu(self):
        url = "%s/cgi-bin/menu/delete?access_token=%s" % (self.host, self.access_token)
        return urllib2.urlopen(url).read()

    @OAuthBase.deco_parse_json_resp
    def send_text(self, openid, text):
        url = "%s/cgi-bin/message/custom/send?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"touser": openid, "msgtype": "text", "text": { "content": text } })
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def send_image(self, openid, mediaid):
        url = "%s/cgi-bin/message/custom/send?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"touser": openid, "msgtype": "image", "image": { "media_id": mediaid}})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def send_voice(self, openid, mediaid):
        url = "%s/cgi-bin/message/custom/send?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"touser": openid, "msgtype": "voice", "voice": { "media_id": mediaid}})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def send_video(self, openid, mediaid, title, desc):
        url = "%s/cgi-bin/message/custom/send?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"touser": openid, "msgtype": "video", "video": 
            {"media_id": mediaid, "title": title, "description": desc}})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def send_music(self, openid, music_dict):
        url = "%s/cgi-bin/message/custom/send?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"touser": openid, "msgtype": "music", "music": music_dict})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def send_news(self, openid, articles):
        url = "%s/cgi-bin/message/custom/send?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"touser": openid, "msgtype": "news", "news": { "articles": articles}})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def send_mpnews(self, openid, mediaid):
        url = "%s/cgi-bin/message/custom/send?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"touser": openid, "msgtype": "mpnews", "mpnews": { "media_id": mediaid}})
        return urllib2.urlopen(url, data).read()


    @OAuthBase.deco_parse_json_resp
    def send_template(self, openid, template_id, jump_url, top_color, data):
        url = "%s/cgi-bin/message/template/send?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"touser": openid, "template_id": template_id, "url": jump_url, "topcolor": top_color, "data": data})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def masssend_mpnews(self, openids, media_id):
        url = "%s/cgi-bin/message/mass/send?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"touser": openids, "msgtype": "mpnews", "mpnews": { "media_id": media_id}})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def sendall_mpnews(self, media_id, group_id):
        url = "%s/cgi-bin/message/mass/sendall?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"msgtype": "mpnews", "mpnews": { "media_id": media_id}, 'filter': {'group_id': group_id}})
        print data
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def delete_masssend(self, msg_id):
        url = "%s/cgi-bin/message/mass/delete?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"msg_id": msg_id})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def add_card(self, data):
        url = "%s/card/add?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json(data)
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def get_card(self, card_id):
        url = "%s/card/get?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({'card_id': card_id})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def del_card(self, product_id):
        url = "%s/card/delete?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"product_id": product_id})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def add_shop(self, cid, name, telephone, address, longtitude, latitude):
        url = "%s/card/shop/add?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({"cid": cid, "name": name, "telephone": telephone, "address": address, "longtitude": longtitude, "latitude": latitude})
        #print unicode(data, 'utf8')
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def get_card_by_code(self, code, card_id=''):
        url = "%s/card/code/get?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({'code': code, 'card_id': card_id})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def consume_card_code(self, code, card_id=''):
        url = "%s/card/code/consume?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({'code': code, 'card_id': card_id})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def check_in_boarding_pass(self, code, card_id, data):
        url = "%s/card/boardingpass/checkin?access_token=%s" % (self.host, self.access_token)
        data.update({'code': code, 'card_id': card_id})
        data = self.get_post_json(data)
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def get_location(self, offest, count):
        url = "%s/card/location/batchget?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({'offest': offest, 'count': count})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def get_colors(self):
        url = "%s/card/getcolors?access_token=%s" % (self.host, self.access_token)
        return urllib2.urlopen(url, ' ').read()

    @OAuthBase.deco_parse_json_resp
    def set_white_list(self, openid_list):
        url = "%s/card/testwhitelist/set?access_token=%s" % (self.host, self.access_token)
        data = self.get_post_json({'openid': openid_list})
        return urllib2.urlopen(url, data).read()

    @OAuthBase.deco_parse_json_resp
    def activate_membercard(self, code, card_id, data):
        url = "%s/card/membercard/activate?access_token=%s" % (self.host, self.access_token)
        data.update({'code': code, 'card_id': card_id})
        data = self.get_post_json(data)
        return urllib2.urlopen(url, data).read()


class MMOpenPayApi(MMOpenApi):
    def __init__(self, appid, secret, pay_sign_key):
        super(MMOpenPayApi, self).__init__(appid, secret)
        self.__pay_sign_key = pay_sign_key

    def get_sign(self, dc):
        import hashlib
        dc = deepcopy(dc)
        dc['appkey'] = self.__pay_sign_key
        keys = dc.keys()
        keys.sort()
        content = '&'.join([ '%s=%s' % (key, dc[key] ) 
                        for key in keys])
        sign_gen = hashlib.sha1()
        sign_gen.update(content)
        return sign_gen.hexdigest()

    def get_post_json(self, dc):
        dc["app_signature"] = self.get_sign(dc)
        dc["sign_method"] = "sha1"
        return MMOpenApi.get_post_json(self, dc)

    @OAuthBase.deco_parse_json_resp
    def send_order_msg(self, openid, template_id, transid, out_trade_no, jump_url, data):
        data = MMOpenApi.get_post_json(self, data)
        url = "%s/pay/ordermsg?access_token=%s" % (self.host, self.access_token)
        post_data = self.get_post_json({"appid": self.appid, "openid": openid, "template_id": template_id, "transid": transid, "out_trade_no": out_trade_no, "jump_url": jump_url, "data": data})
        return urllib2.urlopen(url, post_data).read()

    @OAuthBase.deco_parse_json_resp
    def send_notify_msg(self, openid, template_id, data):
        data = MMOpenApi.get_post_json(self, data)
        url = "%s/pay/notifymsg?access_token=%s" % (self.host, self.access_token)
        post_data = self.get_post_json({"appid": self.appid, "openid": openid, "template_id": template_id, "data": data})
        #print post_data
        return urllib2.urlopen(url, post_data).read()
    


test_open_id = 'o1Pj9jmZvwS7kD38TQBa4aULW2mA'
#test_open_id = 'o1Pj9jhQwt1tfDdA4KduGS02Z9g0' #dc
#test_open_id = 'o1Pj9juCIFc_Od6cv1VLgzJvYM4k' #berry

def test_group(api):
    api.get_groups()
    group_id = api.create_group('test_group')['group']['id']
    api.update_group(group_id, u'测试啊啊')
    api.update_group_member(test_open_id, group_id)
    api.get_groups()

def test_qrcode(api):
    #file('ticket.txt', 'w').write(api.get_qrcode_ticket(12321)['ticket'])
    #file('ticket.txt', 'w').write(api.get_limit_qrcode_ticket(1)['ticket'])
    file('ticket.txt', 'w').write(api.get_card_qrcode_ticket('pDF3iYzcRCp2rUslx-k6CEbk_eto')['ticket'])
    #file('ticket.txt', 'w').write(api.get_card_qrcode_ticket('pDF3iYyfsFn-kwSdeK7msFT7shD4', '1112348233')['ticket'])
    pass

def test_user_info(api):
    print api.get_userinfo(test_open_id, "zh_TW")
    #print api.get_all_fans()

def test_media(api):
    #media_id = api.upload_media('test.amr', 'voice')['media_id']
    #file('test.amr', 'wb').write(api.get_media(media_id))

    media_id = api.upload_media('thumb.jpg', 'image')['media_id']
    data = {'articles': [{'thumb_media_id': media_id, 'title': 'title', 'digest': 'digest!!!', 'content': 'conent!!!!', 'content_source_url': 'http://www.baidu.com', 'author': 'jackylian'},
        {'thumb_media_id': media_id, 'title': 'title', 'digest': 'digest!!!', 'content': 'conent!!!!', 'content_source_url': 'http://www.baidu.com', 'author': 'jackylian'}]}
    #print api.get_post_json(data)
    print api.upload_media_news(data)['media_id']

def test_menu(api):
    menu = {
            "button":[
                {	
                    "type":"click",
                    "name":"今日歌曲",
                    "key":"V1001_TODAY_MUSIC"
                    },
                {
                    "type":"click",
                    "name":"歌手简介",
                    "key":"V1001_TODAY_SINGER"
                    },
                {
                    "name":"菜单",
                    "sub_button":[
                        {	
                            "type":"view",
                            "name":"搜索",
                            "url":"http://www.soso.com/"
                            },
                        {
                            "type":"view",
                            "name":"视频",
                            "url":"http://v.qq.com/"
                            },
                        {
                            "type":"click",
                            "name":"赞一下我们",
                            "key":"V1001_GOOD"
                            }]
                        }]
                }
    api.create_menu(menu)
    api.get_menu()
    api.delete_menu()

def test_send(api):
    #print '------send text-------'
    #api.send_text(test_open_id, '哈哈哈哈，测试')

    print '------send voice-------'
    media_id = api.upload_media('test2.amr', 'voice')['media_id']
    api.send_voice(test_open_id, media_id)

    #print '------send image-------'
    #media_id = api.upload_media('test.jpg', 'image')['media_id']
    #api.send_image(test_open_id, media_id)

    #print '------send video-------'
    #video_media_id = api.upload_media('test_jony.mp4', 'video')['media_id']
    #api.send_video(test_open_id, video_media_id, "", "")

    #print '------send music--------'
    #thumb_media_id = api.upload_media('thumb.jpg', 'image')['media_id']
    #music = {
    #  "title": "stronger",
    #  "description": "stronger - kelly clarkson",
    #  "musicurl": "http://sk.36dj.com/up/mp3/9106D0F088E0B04757F8A4CB06BA08EC.mp3",
    #  "hqmusicurl": "http://sk.36dj.com/up/mp3/9106D0F088E0B04757F8A4CB06BA08EC.mp3",
    #  "thumb_media_id": thumb_media_id 
    #}
    #api.send_music(test_open_id, music)

    #print '------send news--------'
    #articles = [
    #     {
    #         "title": "title1",
    #         "description": "description1",
    #         "url": "http://mp.weixin.qq.com",
    #         "picurl": "https://res.wx.qq.com/mpres/htmledition/images/mp_qrcode195d31.png"
    #     },
    #     {
    #         "title": "标题2",
    #         "description": u"unicode 描述2",
    #         "url": "http://mp.weixin.qq.com",
    #         "picurl": "https://res.wx.qq.com/mpres/htmledition/images/mp_qrcode195d31.png"
    #     }
    #     ]
    #api.send_news(test_open_id, articles)

    #print '------send template news--------'
    #data =  {"Goods": "苹果","Unit_price": "RMB 20.13","Quantity": "5","Total": "RMB 100.65","Source": {"Shop": "Jas\n屌丝商店","Recommend": "5颗星"}}
    #api.send_template(test_open_id, "dgxHFdJDH9e3W9TqC_lm8H9cZfIhuL7MQ1SQPxlDllcDnfZcgMlGZ43Se3Nv7o3G", "http://weixin.qq.com/download", "#00FF00", data)

    #data =  {"Goods": {"value": "苹果", "color": "#173177"}, "Unit_price": {"value": "RMB 20.13", "color": "#323433"}, "Quantity": {"value": "5"}, "Total": {"value": "RMB 100.65"}, 
    #        "Source": {"Shop": {"value": "Jas\n屌丝商店"}, "Recommend": {"value": "5颗星"}}}
    #api.send_template(test_open_id, "Etk_pt3gtcNjiJQid0sPXURD_lUuZ-HxklCt6iuQuxg", "http://weixin.qq.com/download", "#00FF00", data)

    #print '------send mpnews-------'
    #api.send_mpnews(test_open_id, '5vYa57thOEVP9CO-5m-nMtOoeEhAddNoyZTTlgpRydMsI8En1EMHRQefTBEh1kP7')
    pass

def test_pay(api):
    #print '------send order msg--------'
    #data = "" 
    #api.send_order_msg(test_open_id, "6HtIS9Lz6ruuYmcsNT59O3KaDTgVff9WjVWskl3vq28", "1217576101201401060437571277", "out_trade_no1",  "http://www.baidu.com", data)

    #data = {'logistics_company': '顺丰快递', 'logistics_order_no': '1233321123434234'}
    #api.send_order_msg(test_open_id, "mQZJ4nuz3Yzy5M09f3iVItaqt50xM_4xxRb-qR8h6eY", "1217576101201401060437571277", "out_trade_no1",  "http://www.baidu.com", data)

    print '------send notify msg--------'
    #data = {'button_name': '点我啊笨', 'button_url': 'http://weixin.qq.com'}
    #api.send_notify_msg(test_open_id, 'QyVALC12kzcFnf3YtQ7FT4aMxBLN1U_5qzQv6m9IwGw', data);
    api.send_notify_msg(test_open_id, 'n1aVbPkI7y0_qq27ekeQBmR5oLK8ZeCZgvbJ8uJX8ac', {'recharge_phonenumber': '88888888', 'chargeremind_url': '<![CDATA[weixin://wxpay/jumpurl?action=3&functionid=502318]]>'});
    pass

def test_masssend(api):
    print '------sendall--------'
    #api.sendall_mpnews('6E0DO_9W8wQ48gqyeRppxwGeJ0iWGN2sdYtfTZXNOnNJ5154JODtcRPRF43vZIla', 2)

    #api.masssend_mpnews([test_open_id, test_open_id], '-DVSp4gIG11BR_4Big2s3Dx3lSURdqx6UGe8lE_9AeW4CIrsOBVl7UZ0LHK65pg-')
    #api.delete_masssend(38472)

def test_card(api):
    #print '------add card--------'
    #data = file('data/coupon_haidilao.json').read()
    #data = file('data/coupon_star_bucks.json').read()
    ##data = file('data/coupon_mcdonald\'s.json').read()
    #data = file('data/boardingpass.json').read()
    #data = json.loads(data)
    #print data
    #card_id = api.add_card(data)['card_id']
    #card_id = 'p1Pj9jqDsQrxGWPXfdrqFAnBEdWU'
    #file('ticket.txt', 'w').write(api.get_card_qrcode_ticket(card_id, "1233211a3", test_open_id)['ticket'])
    #file('ticket.txt', 'w').write(api.get_card_qrcode_ticket(card_id, is_unique_code=True)['ticket'])
    #file('ticket.txt', 'w').write(api.get_card_qrcode_ticket(card_id)['ticket'])

    #print '------get card--------'
    #card_id = 'p1Pj9jidjc9jNOweYdQUDhkicydI'
    #api.get_card(card_id)

    #print '------del card--------'
    #api.del_card(product_id)

    #print '------add shop--------'
    #print api.add_shop(1, '森林小铺', '13570407790', '沉睡森林1024号', 43.34, 212.4)

    #print '------get card by code--------'
    #api.get_card_by_code('50459977558293880832', 'pFS7FjuBTWZHO2IYzjmcYTh-LXSQ')

    #print '------consume card code--------'
    #card_id = 'p1Pj9jgXPuuuoo4jr2Tvpl_UblWo'
    #api.consume_card_code('1233211a3', card_id)

    #print '------check in--------'
    data = {
       'passenger_name': '逗逼',
       'class': '头等舱',
       'gate': 'A16',
       'seat': 'K22',
       'etkt_bnt': '123123123a',
       'qrcode_data': '123123 123124 adfasdf 123124 vadfff asdfasfew2r2  2rqreqwvgdavf afwerfwqefqawf',
    }
    #api.check_in_boarding_pass('866613992241', '', data)

    #print '--------get location-------'
    #api.get_location(0, 0)

    #api.get_colors()
    #api.set_white_list([test_open_id])

    print '---------activate member card------'
    data = {
       'init_bonus': 100,
       'init_balance': 200,
       'membership_number': 'AAA00000001',
    }
    api.activate_membercard('252147891338', '', data)

if __name__ == "__main__":
    #api = MMOpenApi('wx588def6b0089dd48', 'da65eb2a3e2aecd33ad890e40d3e7393')
    api = MMOpenApi('wxf8b4f85f3a794e77', 'e4baef594fd2d90b076370a53dddf0d6')
    #test_user_info(api)
    #test_menu(api)
    #test_group(api)
    #test_qrcode(api)
    #test_send(api)
    #test_media(api)

    #test_masssend(api)
    test_card(api)

    #paykey = "m0HHTZ56W9gWid4iX711aN08SscbPFTNSb4QJd2LpTgNFpbGAKMl7v2zyOkZ3Olw6uspg5Jgwy0MyU2HfUBV1d6EBvHJU9OyIPw3tloAu8W8A4nTwAp8oBl6FBT0jggC"    
    #api = MMOpenPayApi('wx588def6b0089dd48', 'da65eb2a3e2aecd33ad890e40d3e7393', paykey)
    #test_pay(api)

    #api.send_text(test_open_id, r'<a href="http://www.mobvoi.com/">test</a>')

    #file('ticket.txt', 'w').write(api.get_limit_qrcode_ticket(-12354)['ticket'])
